import time
from appium import webdriver
from appium.options.android import UiAutomator2Options
from appium.webdriver.common.appiumby import AppiumBy
# pip install Appium-Python-Client
from loguru import logger
from selenium.webdriver import ActionChains
from selenium.webdriver.common.actions import interaction
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.actions.pointer_input import PointerInput
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions

capabilities = {
    "platformName": "Android",
    "appium:platformVersion": "12.0",
    "appium:deviceName": "emulator-5554",
    "appium:appPackage": "cn.kmob.screenfingermovelock",
    "appium:appActivity": "com.samsung.ui.FlashActivity",
    "appium:noReset": True,
    "appium:dontStopAppOnReset": True,
    "appium:autoGrantPermissions": True,
    "forceAppLaunch": True,
    "appium:androidUIAutomator": "androidUIAutomator2",
    "appium:automationName": "UIAutomator2",
    "appium:adbExecTimeout": "80000"
}


def get_faker_name():
    import faker
    logger.info('生成模拟数据-姓名')

    fake = faker.Faker(locale='zh_CN')  # 初始化，指定生成中文格式数据
    name = fake.name_female()  # 生成一条电话号码
    return name


def get_faker_number():
    import faker
    logger.info('生成模拟数据-手机号')

    fake = faker.Faker(locale='zh_CN')  # 初始化，指定生成中文格式数据
    phone = fake.phone_number()  # 生成一条电话号码
    return phone


appium_server_url = 'http://localhost:4723'


class Test_app_demo:
    def setup_class(self):
        self.driver = webdriver.Remote(appium_server_url, options=UiAutomator2Options().load_capabilities(capabilities))
        self.wait = WebDriverWait(self.driver, 30)

    def find_ele(self, by, value):
        el = self.wait.until(expected_conditions.element_to_be_clickable((by, value)))
        return el

    def find_eles(self, by, value):
        self.find_ele(by, value)
        elements = self.driver.find_elements(by, value)
        return elements

    def find_ele_when_scroll(self, by, value):
        try:
            return self.driver.find_element(by=by, value=value)
        except Exception:
            self.driver.swipe(100, 100, 200, 200, duration=1000)
            self.find_ele_when_scroll(by, value)

    def tearDown_class(self):
        if self.driver:
            self.driver.quit()

    def test_demo_scroll(self):
        # 滑动操作--推荐使用该方法
        # time.sleep(10)
        # 查找通讯录
        el = self.wait.until(expected_conditions.element_to_be_clickable(
            (AppiumBy.XPATH, '//android.widget.TextView[@resource-id="com.tencent.wework:id/h1o" and @text="通讯录"]')))

        el.click()
        # time.sleep(2)
        # 添加成员
        # el = self.find_ele_when_scroll(AppiumBy.XPATH,
        #                                '//android.widget.TextView[@resource-id="com.tencent.wework:id/mid1Txt" and @text="添加成员"]')
        time.sleep(2)
        self.driver.swipe(300, 300, 100, 100, duration=500)
        while 1:
            time.sleep(5)
            # 从屏幕四分之三的位置滑动到四分之一的位置
            self.driver.swipe(self.driver.get_window_size()['width'] / 2,
                              self.driver.get_window_size()['height'] * 3 / 4,
                              self.driver.get_window_size()['width'] / 2,
                              self.driver.get_window_size()['height'] * 1 / 4, 2000)  # 通过坐标进行滑动操作滑动时间为3秒
            time.sleep(5)
            self.driver.swipe(self.driver.get_window_size()['width'] / 2,
                              self.driver.get_window_size()['height'] * 1 / 4,
                              self.driver.get_window_size()['width'] / 2,
                              self.driver.get_window_size()['height'] * 3 / 4, 2000)  # 通过坐标进行滑动操作滑动时间为3秒

    def defined_move_mouse(self,  start_move, move_tuple_list, duration):
        touch_input = PointerInput(interaction.POINTER_TOUCH, 'touch')
        start_x, start_y = start_move

        actions = ActionChains(self.driver)
        actions.w3c_actions = ActionBuilder(self.driver, mouse=touch_input)

        actions.w3c_actions.pointer_action.move_to_location(start_x, start_y)
        actions.w3c_actions.pointer_action.pointer_down()
        if duration > 0:
            actions.w3c_actions = ActionBuilder(self.driver, mouse=touch_input, duration=duration)
        for i in move_tuple_list:
            end_x, end_y = i
            actions.w3c_actions.pointer_action.move_to_location(end_x, end_y)

        actions.w3c_actions.pointer_action.release()
        actions.perform()

    def test_demo(self):
        # 进入手势解锁-滑动解锁页面
        ele = self.find_ele(by=AppiumBy.ID, value='cn.kmob.screenfingermovelock:id/topLayout')
        ele.click()
        time.sleep(3)
        # 手势滑动:这个swipe方法其实是一个封装
        # self.driver.swipe(113,184,600,191,duration=2000)
        # 以下是使用w3c进行定制化操作
        self.defined_move_mouse( start_move=(100, 180),
                                move_tuple_list=[(600, 188), (110, 673), (600, 678)], duration=2000)
        time.sleep(80000)
